//
//  MyICRemarkView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/20.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "MyICRemarkView.h"

@implementation MyICRemarkView
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame title:title];
    if (self) {
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(self.titleLabel.x, self.titleLabel.bottom, self.width - 2 * self.titleLabel.x, self.height - self.titleLabel.bottom - 6 * [UIScreen zj_screenHScale])];
        self.bgView .layer.cornerRadius = normalCornerRadius;
        self.bgView .layer.borderWidth = normalBorderWidth;
        self.bgView .layer.borderColor = [UIColor textBlackColor].CGColor;
        
        [self addSubview:self.bgView ];
        [self addSubview:self.textView];
    }
    return self;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [self.textView resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];

    if (toBeString.length >= self.maxNumber && range.length!= 1 && self.maxNumber > 0){
        textView.text = [toBeString substringToIndex:self.maxNumber];
        return NO;
        
    }
    return YES;
}


- (void)setIsShowMust:(BOOL)isShowMust{
    _isShowMust = isShowMust;
    if (isShowMust) {
        self.mustImageView.hidden = NO;
    }
}
- (XXTextView *)textView{
    if (_textView == nil){
        _textView = [[XXTextView alloc] initWithFrame:CGRectMake(self.bgView.x + 9 * [UIScreen zj_screenWScale], self.bgView.y + 15 * [UIScreen zj_screenHScale], self.bgView.width - 18 * [UIScreen zj_screenWScale], self.bgView.height - 30 * [UIScreen zj_screenHScale])];
        _textView.textColor = [UIColor textBlackColor];
        _textView.font = [UIFont titleTextFont];
        _textView.xx_placeholder = @"请输入内容....";
        _textView.returnKeyType = UIKeyboardTypeTwitter;
        _textView.delegate = self;
    }
    return _textView;
}
@end
